/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#ifndef __RANGING_DISPLAY_HPP_
#define __RANGING_DISPLAY_HPP_

#include "RangingClient.hpp"
#include "RangingTypes.hpp"
#include "PositionTypes.hpp"
#include "PositionData.hpp"
#include "databacklog.hpp"
namespace MaCI
{
  namespace Position
  {
    class CPositionClient;
  }
}

class CRangingDisplay : public MaCI::Ranging::CRangingClientCallback, 
                        private gim::CSync
{
  public:
    CRangingDisplay();
    
    /** Draw
     * 
     * Draw current ranging and history (if enabled) to position from position history.
     */
    void Draw();
    
    /** Draw
     * 
     * Draw distance array to position defined by parameters
     * 
     */
    void Draw(const float x, const float y, const float heading, const MaCI::Ranging::TDistanceArray *distances=NULL);
    
    void OnRangingDataEvent(const MaCI::Ranging::CRangingData& data, unsigned int seq,
                            MaCI::Ranging::CRangingClient& client);
    
    void SetZ(float z) {this->z=z;}
    
    float GetZ() {return z;}
    
    //void SetPosition(const MaCI::Position::CPositionData &position);
    
    void SetReferencePosition(MaCI::Position::CPositionClient *positionClient);
    
    //gets device information from server
    void SetDeviceInformation(const MaCI::Ranging::TDeviceInformationLimits limits,
                              const MaCI::Ranging::TDeviceInformationPosition devicePosition);
    
    void SetHistoryLengthMs(int historyLength_ms);
    
    void EnableHistory(bool history);
    
    bool IsHistoryEnabled();
    
  private:
    static const int backlogMutex;
    static const int historyMutex;
    
    struct RangingWPose
    {
      MaCI::Ranging::TDistanceHeader header;
      MaCI::Ranging::TDistanceArray distances;
      MaCI::Position::TPose2D pose;
      gim::time time;
    };
    
    float z;
    bool displayDots;
    bool displayClearance;
    float clearanceAlpha;
    bool displayHistory;
    float historyLifetime_ms;
    MaCI::Position::CPositionClient *referencePosition;
    //gim::CDataBacklog<MaCI::Position::TPose2D> poseBacklog;
    std::list<RangingWPose> history;
    
    MaCI::Ranging::TDeviceInformationLimits limits;
    MaCI::Ranging::TDeviceInformationPosition devicePosition;
    
};

#endif //__RANGING_DISPLAY_HPP_
